package lumis.portal.serviceinstance.serialization;

import java.util.Map;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.progress.ProgressConfig.Type;
import lumis.portal.serialization.Dependency;
import lumis.portal.serialization.SerializationContext;
import lumis.portal.transaction.ITransaction;

import org.w3c.dom.Document;

/**
 * Context class for service instances serialization 
 *
 * @version $Revision: 9947 $ $Date: 2008-12-04 13:35:12 -0200 (Thu, 04 Dec 2008) $
 * @since 4.0.8
 */
public class ServiceInstanceSerializationContext extends ServiceInstanceBaseSerializationContext
{
	private final SerializationContext serializationContext;

	public ServiceInstanceSerializationContext(final SerializationContext serializationContext)
	{
		super(serializationContext);
		this.serializationContext = serializationContext;
	}

	public SerializationContext getSerializationContext()
	{
		return serializationContext;
	}

	@Override
	public SessionConfig getSessionConfig()
	{
		return serializationContext.getSessionConfig();
	}

	@Override
	public ITransaction getTransaction()
	{
		return serializationContext.getTransaction();
	}

	public void addDependency(String key, Dependency dependency)
	{
		Map<String, Dependency> externalServiceInstanceDependencies = serializationContext.getExternalServiceInstanceDependencies();
		if (externalServiceInstanceDependencies.containsKey(key))
			throw new IllegalStateException("Could not add dependency for service instance '" + serviceInstanceId + ": Dependency key '" + key + "' already in use.");
		externalServiceInstanceDependencies.put(key, dependency);
	}
	
	public boolean hasDependency(String key)
	{
		return serializationContext.getExternalServiceInstanceDependencies().containsKey(key);
	}
	
	public void sendProgressNotification(String message, Type type) throws PortalException
	{
		serializationContext.sendProgressNotification(message, type, null);
	}

	public void sendProgressNotification(Document messageDoc, String styleFilename, Type type) throws PortalException
	{
		serializationContext.sendProgressNotification(messageDoc, styleFilename, type);
	}
	
	public void sendProgressFinalNotification(String message, Type type) throws PortalException
	{
		serializationContext.sendProgressFinalNotification(message, type);
	}
}
